package com.crm.framework.interceptor.impl;

import cn.hutool.core.util.StrUtil;
import com.crm.common.auth.common.AuthException;
import com.crm.common.auth.common.AuthorizationCodeEnum;
import com.crm.common.ka.core.common.Const;
import com.crm.common.ka.core.redis.Redis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Slf4j
@Component
public class TokenExpireInterceptor implements HandlerInterceptor {


    @Autowired
    private Redis redis;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o){

        String token = request.getHeader(Const.TOKEN_NAME);
        log.info("URI:"+request.getRequestURI()+",Admin-Token:"+token);
        if (StrUtil.isNotEmpty(token)) {
            Object user = redis.get(token);
            if(!ObjectUtils.isEmpty(user)){
                return true;
            }
            throw new AuthException(AuthorizationCodeEnum.AUTHORIZATION_LOGIN);
        }
        return true;
    }

}
